package com.flycat.common.util;

import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.util.Date;

/**
 * Created by linkun on 2017/9/4.
 */
public class DateUtils {

    public static String getFlightDate(String dateTime) {
        if (StringUtils.isEmpty(dateTime) || dateTime.length() < 10) {
            return null;
        }

        return StringUtils.substring(dateTime, 0, 10);//日期
    }

    public static String getFlihtTime(String dateTime) {
        if (StringUtils.isEmpty(dateTime) || dateTime.length() < 11) {
            return null;
        }

        return StringUtils.substring(dateTime, 11);//时间
    }

    public static String dateToStr(Date date, String formatStr) {
        DateTime dateTime = toDateTime(date);
        return format2String(dateTime, formatStr);
    }


    private static DateTime toDateTime(Date date) {
        if (date == null) {
            return null;
        }
        return new DateTime().withMillis(date.getTime());
    }

    private static DateTimeFormatter getFormatter(String pattern) {
        if (StringUtils.isNotEmpty(pattern)) {
            return DateTimeFormat.forPattern(pattern);
        }
        return null;
    }

    private static String format2String(DateTime time, String pattern) {
        if (time == null || StringUtils.isEmpty(pattern)) {
            return null;
        }
        return time.toString(pattern);
    }

    public static Date addHours(Date date, Integer advanceHour) {
        DateTime dateTime = new DateTime(date);
        DateTime dateTime1 = dateTime.plusHours(advanceHour);
        return dateTime1.toDate();
    }
}
